local arg = {...}

local function restore(chto,kuda)
	local file = fs.open(chto,"r")
	Massiv = textutils.unserialise(file.readAll())
	file.close()
	for i=1,#Massiv do
		if Massiv[i]["text"] == "@empty@empty" then
			fs.makeDir(kuda.."/"..Massiv[i]["path"])
		else
			local file = fs.open(kuda.."/"..Massiv[i]["path"],"w")
			file.write(Massiv[i]["text"])
			file.close()
		end
	end
	shell.run('rm /System/desks/desk.back')
	shell.run('rm /System/accounts/accs.back')
end 

local clear = function(color)
	term.setBackgroundColor(color)
	term.clear()
	term.setCursorPos(1,1)
end
	
local text = function(string,xPos,yPos)
	term.setCursorPos(xPos,yPos)
	term.write(string)
end

if arg[1] == nil then
	os.loadAPI("/System/API/sysbar")
	os.loadAPI('/System/API/contexts')
	local run = true
	local xSize, ySize = term.getSize()
	local xCenter = math.floor(xSize/2)
	local yCenter = math.floor(ySize/2)
	local check = 0
	local find = false
	
	local function backup(chto,vochto)
		local Massiv2 = {}
		if find then
			shell.run('rm /backup.rom')
		end
		local function FileList(path)
			local Massiv = fs.list(path)
			if #Massiv==0 then
				local Massiv2Id = #Massiv2 + 1
				Massiv2[Massiv2Id] = {}
				Massiv2[Massiv2Id]["path"] = path
				Massiv2[Massiv2Id]["text"] = "@empty@empty"
			else
				if check == 0 then
					local f = fs.open('/System/desks/desk.back','w')
					f.write('{...}')
					f.close()
					local f = fs.open('/System/accounts/accs.back','w')
					f.write('{...}')
					f.close()
					for i=1,#Massiv do
						if fs.isDir(path.."/"..Massiv[i]) and Massiv[i] ~= "/rom" and Massiv[i] ~= "rom" and (string.find(path.."/"..Massiv[i],'System') ~= nil or string.find(path.."/"..Massiv[i],'sys') ~= nil or string.find(path.."/"..Massiv[i],'.ds') ~= nil) then
							FileList(path.."/"..Massiv[i])
						elseif not fs.isDir(path.."/"..Massiv[i]) and Massiv[i] ~= "/rom" and Massiv[i] ~= "rom" and (string.find(path.."/"..Massiv[i],'startup') ~= nil or string.find(path.."/"..Massiv[i],'System') ~= nil or string.find(path.."/"..Massiv[i],'sys') ~= nil) and (string.find(path.."/"..Massiv[i],'desks') == nil or string.find(path.."/"..Massiv[i],'desks/desk.back') ~= nil) and (string.find(path.."/"..Massiv[i],'accounts') == nil or string.find(path.."/"..Massiv[i],'accounts/accs.back') ~= nil) and string.find(path.."/"..Massiv[i],l_login..'.png') == nil then
							local Massiv2Id = #Massiv2 + 1
							Massiv2[Massiv2Id] = {}
							Massiv2[Massiv2Id]["path"] = path.."/"..Massiv[i]
							local file = fs.open(path.."/"..Massiv[i],"r")
							Massiv2[Massiv2Id]["text"] = file.readAll()
							file.close()
						end
					end
				elseif check == 2 then
					for i=1,#Massiv do
						if fs.isDir(path.."/"..Massiv[i]) and Massiv[i] ~= "/rom" and Massiv[i] ~= "rom" then
							FileList(path.."/"..Massiv[i])
						elseif not fs.isDir(path.."/"..Massiv[i]) and Massiv[i] ~= "/rom" and Massiv[i] ~= "rom" and Massiv[i] ~= "backup.rom" then
							local Massiv2Id = #Massiv2 + 1
							Massiv2[Massiv2Id] = {}
							Massiv2[Massiv2Id]["path"] = path.."/"..Massiv[i]
							local file = fs.open(path.."/"..Massiv[i],"r")
							Massiv2[Massiv2Id]["text"] = file.readAll()
							file.close()
						end
					end
				end
			end
		end
		FileList(chto)
		file=fs.open(vochto..".rom","w")
		file.write(textutils.serialise(Massiv2))
		file.close()
		shell.run('rm /System/desks/desk.back')
		shell.run('rm /System/accounts/accs.back')
		return Massiv2
	end
	
	local sysBar = function()
		sysbar.header("Backup",mainTextCol,mainColor)
		sysbar.draw(colors.white,colors.black,noty,true,find)
	end
	
	local redraw = function()
		clear(colors.white)
		term.setTextColor(colors.black)
		text("Only system files",3,4)
		text("All data",3,6)
		term.setTextColor(colors.lightGray)
		text("( )",xSize-4,4)
		text("( )",xSize-4,6)
		term.setTextColor(style)
		text(string.char(7),xSize-3,check+4)
		term.setTextColor(colors.lime)
		local f = fs.list('/')
		find = false
		for i=1,#f do
			if f[i]=='backup.rom' then find = true end
		end
		if find then
			text('Restore',xCenter-2,ySize-2)
				term.setTextColor(colors.lightGray)
		end
		text('Backup',xCenter-2,ySize-4)
		sysBar()
	end
	
	
	local running = function()
		while run do
			event, side, x, y = os.pullEvent()
			if event == "mouse_click" then
				if sysbar.back(x,y) then
					term.clear()
					term.setCursorPos(1,1)
					run = false
				elseif sysbar.home(x,y) then
					home = true 
					term.clear()
					term.setCursorPos(1,1)
					run = false
				elseif y==4 then
					check = 0
				elseif y==6 then
					check = 2
				elseif y==ySize-4 and x>=xCenter-2 and x<=xCenter+3 then
					clear(colors.gray)
					term.setTextColor(colors.white)
					text('Backup...',xCenter-4,yCenter)
					backup('/','backup')
					local error = 'Something went wrong'
					if fs.getSize('/backup.rom') < 2 then
						windows.error(error)
					else
						windows.attention({"Success!",},{'Backup completed'})
					end
				elseif y==ySize-2 and x>=xCenter-2 and x<=xCenter+4 and find then
					clear(colors.white)
					restore('/backup.rom','/')
					os.reboot()
				elseif x==xSize and y==ySize and find then
					term.setTextColor(colors.lightGray)
					term.setBackgroundColor(colors.gray)
					text('|',xSize,ySize)
					local but = contexts.main(xSize,ySize-1,'Remove backup')
					if but == 'Remove backup' then
						redraw()
						local bu = windows.select({"Dellete backup"},{"Are you sure?"},{"Yes",colors.red,colors.white},{"No",colors.gray,colors.white})
						if bu == 'Yes' then
							shell.run('rm /backup.rom')
						end
					end
				end
				redraw()
			end
		end
	end
	
	redraw()
	running()
	
elseif arg[1] == '-restore' or arg[1] == '-r' then
	clear(colors.black)
	term.setTextColor(colors.lime)
	text('Restoring...',2,2)
	if arg[2] == nil then
		local file = fs.list('/')
		local find = false
		for i=1,#file do
			if file[i] == 'backup.rom' then find = true end
		end
		if find then 
			restore('/backup.rom','/')
			os.reboot()
		else
			term.setTextColor(colors.red)
			text('ERROR: no matching file',2,4)
			term.setCursorPos(1,6)
		end
	else
		local file = fs.list('/')
		local find = false
		for i=1,#file do
			if file[i] == arg[2] then find = true end
		end
		if find then 
			restore(arg[2],'/')
			os.reboot()
		else
			term.setTextColor(colors.red)
			text('ERROR: no matching file',2,4)
			term.setCursorPos(1,6)
		end
	end
end